package com.sunxueqi.project.system.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sunxueqi.common.constant.CfnConstants;
import com.sunxueqi.common.constant.StatusConstants;
import com.sunxueqi.common.utils.KeyGenerate;
import com.sunxueqi.common.utils.SecurityUtils;
import com.sunxueqi.project.system.dao.MenuMapper;
import com.sunxueqi.project.system.service.IDictInfoService;
import com.sunxueqi.project.system.service.IMenuService;
import com.sunxueqi.project.system.vo.DictInfo;
import com.sunxueqi.project.system.vo.DictType;
import com.sunxueqi.project.system.vo.Menu;
import com.sunxueqi.project.system.vo.Meta;
import com.sunxueqi.project.system.vo.NaiveUIRouter;
import com.sunxueqi.project.system.vo.Router;
import com.sunxueqi.project.system.vo.User;

import cn.hutool.core.convert.impl.BooleanConverter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;

@Service
public class MenuServiceImpl implements IMenuService {

	@Autowired
	private MenuMapper menuMapper;

	@Override
	public List<String> selectPermissionsByUser(User user) {
		List<String> list = menuMapper.selectPermissionsByUserId(user.getId());
		return list;
	}
	
	@Override
	public List<Menu> selectMenuTreeByUserId(String userId) {
		List<Menu> menus;
		if (SecurityUtils.isAdmin()) {
			menus = menuMapper.selectMenuTreeAll();
		} else {
			menus = menuMapper.selectMenusByUserId(userId);
		}
		return getChildrenPerms(menus, "0");
	}

	/**
	 * 根据父节点的id获取所有子节点
	 * 
	 * @param list
	 * @param parentId
	 * @return
	 */
	public List<Menu> getChildrenPerms(List<Menu> list, String parentId) {
		List<Menu> returnList = new ArrayList<>();

		for (Menu menu : list) {
			if (menu.getParentId().equals(parentId)) {
				recursionFn(list, menu);
				returnList.add(menu);
			}
		}
		return returnList;
	}

	/**
	 * 递归列表
	 * 
	 * @param list
	 * @param menu
	 */
	private void recursionFn(List<Menu> list, Menu menu) {
		List<Menu> childList = getChildList(list, menu);
		menu.setChildren(childList);
		for (Menu menu2 : childList) {
			if (hasChild(list, menu2)) {
				recursionFn(list, menu2);
			}
		}
	}

	/**
	 * 得到子节点列表
	 * 
	 * @param list
	 * @param menu
	 * @return
	 */
	private List<Menu> getChildList(List<Menu> list, Menu menu) {
		List<Menu> returnList = new ArrayList<Menu>();
		for (Menu m : list) {
			if (m.getParentId().equals(menu.getId())) {
				returnList.add(m);
			}
		}
		return returnList;
	}

	/**
	 * 判断是否有子节点
	 * 
	 * @param list
	 * @param menu
	 * @return
	 */
	private boolean hasChild(List<Menu> list, Menu menu) {
		return !getChildList(list, menu).isEmpty();
	}

	@Override
	public List<Router> buildMenus(List<Menu> menus) {
		
		List<Router> routers = new LinkedList<Router>();
		for (Menu menu : menus) {
			Router router = new Router();
			router.setName(menu.getPath());
			router.setPath(getRouterPath(menu));
			router.setComponent(StrUtil.isEmpty(menu.getComponent()) ? "Layout" : menu.getComponent());
//			System.out.println("menu:" + menu.getMenuName() + "---" + menu.getKeepAlive() + "---" + isKeepAlive(menu.getKeepAlive()));
			router.setMeta(new Meta(menu.getMenuName(), menu.getIcon(), menu.getIframeSrc(), isKeepAlive(menu.getKeepAlive())));
//			System.out.println(router.getName()+router.getMeta().getKeepAlive());
			router.setHidden(StrUtil.equals(menu.getVisible(), StatusConstants.HIDDEN));
			List<Menu> childrenMenus = menu.getChildren();
			if (!childrenMenus.isEmpty()) {
				router.setAlwaysShow(true);
//				router.setRedirect("noRedirect");
				router.setRedirect("/" + menu.getPath() + "/" + childrenMenus.get(0).getPath());
				router.setChildren(buildMenus(childrenMenus));
			}
			routers.add(router);
		}
		return routers;
	}
	
	private boolean isKeepAlive(String keepAlive) {
		if(keepAlive == null) return false;
		return keepAlive.equals(StatusConstants.YES) ? true : false;
	}
	
//	@Override
//	public List<NaiveUIRouter> buildMenusForNaiveUI(List<Menu> menus) {
//		List<NaiveUIRouter> routers = new LinkedList<NaiveUIRouter>();
//		
//		for (Menu menu : menus) {
//			NaiveUIRouter router = new NaiveUIRouter();
//			router.setKey(menu.getPath());
//			router.setLabel(menu.getMenuName());
//			router.setName(menu.getMenuName());
//			router.setPath(getRouterPath(menu));
//			router.setComponent(StrUtil.isEmpty(menu.getComponent()) ? "Layout" : menu.getComponent());
//			router.setMeta(new Meta(menu.getMenuName(), menu.getIcon(), menu.getIframeSrc(), menu.getKeepAlive()));
//			List<Menu> childrenMenus = menu.getChildren();
//			if (!childrenMenus.isEmpty()) {
////				router.setAlwaysShow(true);
////				router.setRedirect("noRedirect");
//				router.setChildren(buildMenusForNaiveUI(childrenMenus));
//			}
//			routers.add(router);
//		}
//		return routers;
//	}
	
	
	

	public String getRouterPath(Menu menu) {
		String routerPath = menu.getPath();
		if (menu.getParentId().equals("0")) {
			routerPath = "/" + menu.getPath();
		}
		return routerPath;
	}
	
	@Override
	public List<Menu> selectMenuList(Menu menu) {
		menu.setDeleteStatus(StatusConstants.NORMAL);
		return menuMapper.selectList(menu);
	}

	@Override
	public int add(Menu menu) {
		menu.setId(KeyGenerate.getKeyId());
		menu.setDeleteStatus(StatusConstants.NORMAL);
		menu.setCreateBy(SecurityUtils.getLoginUserId());
		menu.setCreateTime(new Timestamp(System.currentTimeMillis()));
		return menuMapper.insert(menu);
	}

	@Override
	public int deleteMenuByIds(String[] idArray) {
		int result = 0;
		for(String id: idArray) {
			Menu menu = new Menu();
			menu.setId(id);
			menu.setDeleteStatus(StatusConstants.DELETED);
			menu.setDeleteTime(new Timestamp(System.currentTimeMillis()));
			menu.setDeleteBy(SecurityUtils.getLoginUserId());
			int count = menuMapper.updateByPrimaryKey(menu);
			result += count;
		}
		return result;
	}

	@Override
	public int updateMenu(Menu menu) {
		menu.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		menu.setUpdateBy(SecurityUtils.getLoginUserId());
		int count = menuMapper.updateByPrimaryKey(menu);
		return count;
	}

	@Override
	public Menu selectMenuById(String id) {
		return menuMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Menu> selectMenuListByRoleId(String roleId) {
		return menuMapper.selectMenuListByRoleId(roleId);
	}
	

}
